package com.wcarmon.life.command{

    import com.adobe.cairngorm.commands.ICommand
    import com.adobe.cairngorm.control.CairngormEvent;
	import mx.controls.Alert;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.IResponder;
	import com.wcarmon.life.model.LifeModelLocator;
	import com.wcarmon.life.event.*;
	import com.wcarmon.life.business.*;
	import mx.utils.ObjectUtil;
	
		
	public class SaveBlockCommand implements ICommand, IResponder {
		include "../../includes/loggerSnippet.as";
	
		private var callbackScreen : Object;	//Process or Edit 
		
		public function execute( event : CairngormEvent ) : void { 
			var e : SaveBlockEvent = SaveBlockEvent( event );
			callbackScreen = e.callbackScreen;
	
			var delegate : SaveBlockDelegate = new SaveBlockDelegate(this);
			delegate.saveBlock( e.block );
		}
	
	 	public function result( event : Object ) : void {
			var e : ResultEvent = ResultEvent(event);
			
			var success : Boolean = (e.result.errorCode == 0 && e.result.affected_rows == 1);
			var noChange : Boolean = (e.result.errorCode == 0 && e.result.affected_rows == 0);  
			
			if( success ){
				log.info("Saved changes to item #"+e.result.block_id);
				Alert.show("Saved changes to item #"+e.result.block_id);
				callbackScreen.handleSaveCallback();
				
			}else if( noChange ){
				log.info("No changes made to item #" + e.result.block_id );
				Alert.show("No changes made to item #" + e.result.block_id );
				callbackScreen.handleSaveCallback();
				
			}else{
				log.error("Mysql error #"+ e.result.errorCode +" while saving:\n" + e.result.errorDesc );
				log.error("Update Query:\n" + e.result.updateQuery );
				log.error("affected_rows = " + e.result.affected_rows);
				Alert.show("DB error #"+ e.result.errorCode +" while saving!");
			}
		}
	
	
		public function fault( event : Object ) : void {
			include "includes/FaultSnippet.as";
		}
	}
}